package com.qf.controller;

import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsPermissionService;
import com.qf.service.DtsRoleService;
import com.qf.util.Permission;
import com.qf.util.PermissionUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.PermVo;
import javafx.application.Application;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.awt.image.RescaleOp;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/role")
public class DtsAdminRoleController {
    @Autowired
    DtsRoleService roleService;
    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    DtsPermissionService permissionService;
    @PostMapping("/permissions")
    public Object permissions(@RequestBody Map<String,Object> map) {
        Integer roleId = (Integer) map.get("roleId");
        ArrayList<String> permissions = (ArrayList<String>) map.get("permissions");
        if(roleService.isSuperAdmin(roleId)){
            return ResponseUtil.ok();
        }
        permissionService.updatePermissions(roleId, permissions);
        return ResponseUtil.ok();
    }
    @GetMapping("/permissions")
    public Object permissions(Integer roleId) {
        //获取系统权限
        List<Permission> permissions = PermissionUtil.listPermission(applicationContext, "com.qf.controller" );
        //获取 系统权限 列表（转换数据结构）
        List<PermVo> systemPermissions = PermissionUtil.listPermVo(permissions);
        //获取 当前角色拥有的权限
        Set<String> assignedPermissions;
        //判断角色是否是超级管理员
        if(roleService.isSuperAdmin(roleId)){
            //如果是超级管理员，则拥有所有的权限
            assignedPermissions = PermissionUtil.listPermissionString(permissions);
        }
        //否则，需要去数据库查询 匹配的权限
        else{
            assignedPermissions = roleService.queryStringPermissions(roleId);
        }

        Map<String,Object> data = new HashMap<>();
        data.put("systemPermissions" , systemPermissions);
        data.put("assignedPermissions" , assignedPermissions);

        return ResponseUtil.ok(data);
    }
    @GetMapping("/options")
    public Object options() {
        // 查询所有角色信息
        List<DtsRole> roleList = roleService.findAll();
        // 转换数据的格式，与前端匹配
        List<Map<String, Object>> data = roleList.stream().map((role) -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", role.getId());
            map.put("label", role.getName());
            return map;
        }).collect(Collectors.toList());

        return ResponseUtil.ok(data);
    }
    @RequiresPermissions("admin:role:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsRole role){
        roleService.insert(role);

        return ResponseUtil.ok(role);
    }
    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色查询")
    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "limit",required = false, defaultValue = "10") Integer limit ,
            String sort , String order, String rolename
    ) {
        Map<String,Object> data = roleService.queryPage(page, limit, sort, order, rolename);

        return ResponseUtil.ok(data);
    }
    @RequiresPermissions("admin:role:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"}, button = "角色编辑")
    @PostMapping("/update")
    public Object updateRole(@RequestBody DtsRole role) {
        roleService.update(role);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:role:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"}, button = "角色删除")
    @PostMapping("/delete")
    public Object deleteRole(@RequestBody DtsRole role) {
        roleService.delete(role);
        return ResponseUtil.ok();
    }
}